<template>
  <div class="navigation-bar">
    <div class="path-navigator">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item @click="emit('navigate', '/')">根目录</el-breadcrumb-item>
        <el-breadcrumb-item 
          v-for="(path, index) in pathSegments" 
          :key="index"
          @click="emit('navigate', getPathUpToIndex(index))"
        >
          {{ formatPathSegment(path, index) }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="toolbar">
      <el-button-group>
        <el-button 
          @click="emit('upload')" 
          v-permission="'system:oss:doUpload'"
        >
          <el-icon><Upload /></el-icon>上传
        </el-button>
        <el-button 
          @click="emit('createFolder')"
          v-permission="'system:oss:doMkdir'"
        >
          <el-icon><FolderAdd /></el-icon>新建文件夹
        </el-button>
        <el-button @click="emit('refresh')">
          <el-icon><Refresh /></el-icon>刷新
        </el-button>
      </el-button-group>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Upload, FolderAdd, Refresh } from '@element-plus/icons-vue'

const props = defineProps<{
  currentPath: string
}>()

const emit = defineEmits<{
  (e: 'navigate', path: string): void
  (e: 'upload'): void
  (e: 'createFolder'): void
  (e: 'refresh'): void
}>()

// 处理路径段
const pathSegments = computed(() => {
  // 移除开头和结尾的斜杠，然后分割
  const path = props.currentPath.replace(/^\/|\/$/g, '')
  return path ? path.split('/') : []
})

// 格式化路径段显示
const formatPathSegment = (path: string, index: number) => {
  return path
}

// 获取到指定索引的完整路径
const getPathUpToIndex = (index: number) => {
  const segments = pathSegments.value.slice(0, index + 1)
  // 确保路径以斜杠开头和结尾
  return segments.length ? `/${segments.join('/')}/` : '/'
}
</script>

<style scoped lang="scss">
.navigation-bar {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.path-navigator {
  flex: 1;
  margin-right: 16px;
}

:deep(.el-breadcrumb__item) {
  cursor: pointer;
  &:hover {
    color: #409eff;
  }
}
</style> 